//
//  UIDevice+MobileType.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/2.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "UIDevice+MobileType.h"

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

@implementation UIDevice (MobileType)


//2G,3G,4G,WIFI,无服务
+ (NSNetWorkType)netWorkType
{
    NSNetWorkType workType = NSNetWorkType_None;
    
    NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
    NSNumber *dataNetworkItemView = nil;
    
    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }
    
    NSString *mobileText = @"";
    switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
        case 0:
            LLog(@"No wifi or cellular");
            mobileText=@"无服务";
            workType = NSNetWorkType_None;
            break;
            
        case 1:
            LLog(@"2G");
            mobileText=@"2G";
            workType = NSNetWorkType_2G;
            break;
            
        case 2:
            LLog(@"3G");
            mobileText=@"3G";
            workType = NSNetWorkType_3G;
            break;
            
        case 3:
            LLog(@"4G");
            mobileText=@"4G";
            workType = NSNetWorkType_4G;
            break;
            
        case 4:
            LLog(@"LTE");
            mobileText=@"LTE";
            workType = NSNetWorkType_4G;
            break;
            
        case 5:
            LLog(@"Wifi");
            mobileText=@"Wifi";
            workType = NSNetWorkType_Wifi;
            break;
            
        default:
            break;
    }
    return workType;
}
//运营商名字
+ (NSMobileType)mobileName
{
    NSMobileType mobileType = NSMobileType_None;
    
    CTTelephonyNetworkInfo *telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [telephonyInfo subscriberCellularProvider];
    NSString *currentCountry=[carrier carrierName];
    LLog(@"[carrier isoCountryCode]==%@,[carrier allowsVOIP]=%d,[carrier mobileCountryCode=%@,[carrier mobileCountryCode]=%@",[carrier isoCountryCode],[carrier allowsVOIP],[carrier mobileCountryCode],[carrier mobileNetworkCode]);
    LLog(@"currentCountry:%@",currentCountry);
    
    if ([currentCountry isEqualToString:@"China Mobile"])
    {
        mobileType = NSMobileType_CHINAMOBILE;
    }
    else if([currentCountry isEqualToString:@"China Unicom"])
    {
        mobileType = NSMobileType_CHINAUNICOM;
    }
    else if ([currentCountry isEqualToString:@"China Telecom"])
    {
        mobileType = NSMobileType_CHINATELECOM;
    }
   
    return mobileType;
}


@end
